﻿namespace LeetCodeProject._0001_1000._001_100._61_70._061;

public class Solution
{
    public ListNode RotateRight(ListNode head, int k)
    {
        if (head == null)
            return head;
        var dummy = new ListNode(0) { next = head };
        var n = 0;
        var p = head;
        while (true)
        {
            n++;
            if (p.next == null)
            {
                p.next = head;
                break;
            }

            p = p.next;
        }

        k = n - k % n;
        if (k == 0)
            return head;
        p = dummy;
        while (k > 0)
        {
            k--;
            p = p.next;
        }

        head = p.next;
        p.next = null;
        return head;
    }
}

public class ListNode
{
    public int val;
    public ListNode next;

    public ListNode(int val = 0, ListNode next = null)
    {
        this.val = val;
        this.next = next;
    }
}